package uy.com.nfc.localassigner;

import java.util.List;

import uy.com.nfc.localassigner.adapters.RecordsListAdapter;
import uy.com.nfc.localassigner.managers.SessionManager;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.view.WindowManager;
import android.widget.ListView;
import android.widget.TextView;

import com.example.nfctagcommons.classes.PointCheck;
import com.example.nfctagcommons.dialog.ConnectivityErrorDialog;
import com.example.nfctagcommons.dialog.LoadOnBackgroundDialog;
import com.example.nfctagcommons.dialog.LoadOnBackgroundDialog.IBackgroundAction;
import com.example.nfctagcommons.server.ServerConnector;
import com.example.nfctagcommons.utils.SystemConstants;

public class MyRecords extends Activity {
	private static final int[] textViewIds = {
		R.id.backActionBar
	};
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_my_records);
		setTitle(R.string.title_activity_my_records);
		setCustomTypeface();
		getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
		if (!SessionManager.isLoggedIn(this)){
			startActivity(new Intent(getApplicationContext(), MainActivity.class));
			finish();
			return;
		}
		
		LoadOnBackgroundDialog background = new LoadOnBackgroundDialog(this);
		background.load(true, "Obteniendo lista de registros...", new IBackgroundAction() {
			private List<PointCheck> pointList;
			@Override
			public boolean doBackgroundAction() {
				try {
					String guardId = SessionManager.getGuardId(MyRecords.this);
					pointList = ServerConnector.getVigilanceLocalHistory(MyRecords.this, guardId);
					return true;
				} catch (Exception e) {
					e.printStackTrace(); 
					return false;
				}
			}
			@Override
			public void doPostExecuteAction(boolean success) {
				if (success){
					ListView localListView = (ListView)findViewById(R.id.myRecords);
					String[] items = new String[pointList.size()];
					localListView.setAdapter(new RecordsListAdapter(MyRecords.this,items,pointList));
				}else{
					ConnectivityErrorDialog.showDialog(MyRecords.this, MainActivity.class);
				}
			}
			@Override
			public void onCancelled() {
				startActivity(new Intent(getApplicationContext(), MainActivity.class));
				finish();
			}
		});
		
	}
	@Override
	protected void onResume() {
		super.onResume();
		SystemConstants.syncTime(this);
	}
	private void setCustomTypeface() {
		Typeface customTypeface = Typeface.createFromAsset(getAssets(), "fonts/AliquamREG.ttf");
		for (int id : textViewIds){
			((TextView)findViewById(id)).setTypeface(customTypeface);
		}
	}
	public void back(View view){
		onBackPressed();
	}
	@Override
	public void onBackPressed() {
		Intent intent = new Intent(getApplicationContext(), MainActivity.class);
		intent.putExtra(MainActivity.FROM_FROWARD_SCREEN, true);
		startActivity(intent);
		finish();
	}
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
	    switch (item.getItemId()) {
		    case android.R.id.home:
		    	onBackPressed();
		        return true;
	    }
	    return super.onOptionsItemSelected(item);
	}
}
